package com.itmk.system.role.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itmk.config.redis.RedisUtils;
import com.itmk.result.DataResult;
import com.itmk.result.ResultVo;
import com.itmk.system.common.entity.JsonData;
import com.itmk.system.role.entity.Role;
import com.itmk.system.role.service.RoleService;
import com.itmk.system.user_role.entity.UserRole;
import com.itmk.vo.req.RoleReqVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/role")
@Api(tags = "角色管理")
public class RoleController {

    @Autowired
    private RoleService roleService;


    /**
     * 根据id查询角色
     * @return
     */
    @ApiOperation(value = "根据id查询角色")
    @RequestMapping(value = "/getRoleById",method = RequestMethod.POST)
    public DataResult getRoleById(@RequestBody Role role){
        Role sysRole = roleService.getById(role.getId());
        return DataResult.success(sysRole);
    }

    /**
     * 编辑角色保存
     * @return
     */
    @ApiOperation(value = "编辑角色")
    @RequestMapping(value = "/editRole",method = RequestMethod.POST)
    public DataResult editRole(@RequestBody Role role){
        boolean b = roleService.updateById(role);
        if(b){
            return DataResult.success();
        }else{
            return DataResult.getResult(300100,"编辑失败!");
        }
    }

    /**
     * 删除角色
     * @return
     */
    @ApiOperation(value = "删除角色")
    @RequestMapping(value = "/deleteById",method = RequestMethod.POST)
    public DataResult deleteById(@RequestBody Role role){
        boolean b = roleService.removeById(role);
        if(b){
            return DataResult.success();
        }else{
            return DataResult.getResult(300100,"删除失败!");
        }

    }
    /**
     * 查询角色列表
     * @param parmVo
     * @return
     */
    @ApiOperation(value = "查询角色列表")
    @RequestMapping(value = "/getRoleList",method = RequestMethod.POST)
    public DataResult getRoleList(@RequestBody RoleReqVo parmVo){
        QueryWrapper<Role> query = new QueryWrapper<>();
        if(!StringUtils.isBlank(parmVo.getTitle())){
            query.lambda().like(Role::getName,parmVo.getTitle());
        }
        IPage<Role> page = new Page();
        page.setSize(parmVo.getPageSize());
        page.setCurrent(parmVo.getCurrentPage());
        IPage<Role> roleList = roleService.page(page, query);
        return DataResult.success(roleList);
    }

    /**
     * 新增角色
     * @param role
     * @return
     */
    @ApiOperation(value = "新增角色")
    @RequestMapping(value = "/addNewRole",method = RequestMethod.POST)
    public DataResult addNewRole(@RequestBody Role role){
        boolean b=roleService.save(role);
        if(b){
            return DataResult.success();
        }else {
            return DataResult.getResult(300100,"新增失败");
        }
    }

    /**
     * 分配角色角色列表
     * @return
     */
    @ApiOperation(value = "获取全部分配角色列表")
    @RequestMapping(value = "/getRolistForAssing",method = RequestMethod.POST)
    public DataResult getRolistForAssing(){
        List<Role> list = roleService.list();
        return DataResult.success(list);
    }

    /**
     * 根据用户id查询角色id
     * @return
     */
    @RequestMapping(value = "/getRoleIdByUserId",method = RequestMethod.POST)
    public DataResult getRoleIdByUserId(@RequestBody UserRole userRole){
        UserRole roleIdByUserId = roleService.getRoleIdByUserId(userRole.getUserId());
        return DataResult.success(roleIdByUserId);
    }

    /**
     * 分配角色保存
     * @return
     */
    @RequestMapping(value = "/assingRole",method = RequestMethod.POST)
    public DataResult assingRole(@RequestBody UserRole userRole, HttpServletRequest request){
        Boolean b=roleService.assingRole(userRole);
        if(b){
            RedisUtils.ClearRedisMenu(request);
            return DataResult.success();
        }else{
            return DataResult.getResult(300100,"角色分配失败");
        }

    }
}
